<template>
	<view class="container">
		<u-top-tips ref="uTips"></u-top-tips>
		<Steps :stepIndex="2"></Steps>
		<u-form :model="addMerForm" :rules="merFormRules" ref="merFormRef" label-width="5rem">
			<u-form-item label-position="left" label="室内照" prop="interior_photo" label-width="150">
				<u-upload :show-progress="false" action="http://openpay.duolaibei.com/admin/upload/upload" name="image" max-count="1"
				 width="160" height="160" @on-choose-complete="upLoadBefore" @on-success="shineiUpSuccess" @on-error="upLoadImgError"></u-upload>
			</u-form-item>
			<u-form-item label-position="left" label="门头照" prop="sign_doard" label-width="150">
				<u-upload :show-progress="false" action="http://openpay.duolaibei.com/admin/upload/upload" name="image" max-count="1"
				 width="160" height="160" @on-choose-complete="upLoadBefore" @on-success="mentouUpSuccess" @on-error="upLoadImgError"></u-upload>
			</u-form-item>
			<u-form-item label-position="left" label="特殊资质(可选)" label-width="150">
				<u-upload :show-progress="false" action="http://openpay.duolaibei.com/admin/upload/upload" name="image" max-count="1"
				 width="160" height="160" @on-choose-complete="upLoadBefore" @on-success="teshuzizhiUpSuccess" @on-error="upLoadImgError"></u-upload>
			</u-form-item>
			<u-form-item label-position="left" label="补充图片(可选)" label-width="150">
				<u-upload :show-progress="false" action="http://openpay.duolaibei.com/admin/upload/upload" name="image" max-count="1"
				 width="160" height="160" @on-choose-complete="upLoadBefore" @on-success="buchongUpSuccess" @on-error="upLoadImgError"></u-upload>
			</u-form-item>
			<u-form-item label-position="left" label="备注(选填)">
				<u-input v-model="addMerForm.note" type="textarea" :border="true" height="150" :auto-height="true" placeholder="特殊银行备注" />
			</u-form-item>
		</u-form>
		<view class="btns-area">
			<u-button @click="addMer" type="warning" :plain="true">提交</u-button>
		</view>
	</view>
</template>

<script>
	import Steps from '@/components/steps/index.vue'
	export default {
		components: {
			Steps
		},
		data() {
			return {
				merType: 0,
				addFormA: {},
				addFormB: {},
				addMerForm: {
					imgObj: {}
				},
				// 状态类型选择
				statusList: [{
						name: '启用',
						value: 1
					},
					{
						name: '禁用',
						value: 2
					},
				],
				// 语法校验
				merFormRules: {
					interior_photo: [{
						required: true,
						message: '请上传室内照',
						validator: (rule, value, callback) => {
							if (this.addMerForm.imgObj.interior_photo == undefined) {
								return false
							}
							return true
						}
					}, ],
					sign_doard: [{
						required: true,
						message: '请上传门头照',
						validator: (rule, value, callback) => {
							if (this.addMerForm.imgObj.sign_doard == undefined) {
								return false
							}
							return true
						}
					}, ],
				}
			}
		},
		onLoad(options) {
			this.merType = options.type;
			// 获取缓存中的前两个form表单
			this.getForm();
		},
		onReady() {
			this.$refs.merFormRef.setRules(this.merFormRules);
		},
		methods: {
			getForm() {
				this.addFormA = JSON.parse(uni.getStorageSync('addFormA'));
				this.addFormB = JSON.parse(uni.getStorageSync('addFormB'));
			},
			upLoadBefore() {
				uni.showLoading({
					title: '努力上传中',
					mask: true
				});
			},
			shineiUpSuccess(data, index, lists, name) {
				let con = data.content;
				this.addMerForm.imgObj.interior_photo = con.path;
				uni.hideLoading();
			},
			mentouUpSuccess(data, index, lists, name) {
				let con = data.content;
				this.addMerForm.imgObj.sign_doard = con.path;
				uni.hideLoading();
			},
			teshuzizhiUpSuccess(data, index, lists, name) {
				let con = data.content;
				this.addMerForm.imgObj.special_card = con.path;
				uni.hideLoading();
			},
			buchongUpSuccess(data, index, lists, name) {
				let con = data.content;
				this.addMerForm.imgObj.supplement_photo = con.path;
				uni.hideLoading();
			},
			// 图片上传失败
			upLoadImgError() {
				// this.$refs.uTips.show({
				// 	title: '上传失败',
				// 	type: 'error',
				// 	duration: '2000'
				// })
			},

			// 添加商户
			addMer() {
				let that = this,
					form = this.addMerForm;
				this.$refs.merFormRef.validate(valid => {
					if (valid) {
						let imgObj = {};
						Object.assign(imgObj, that.addFormA.imgObj, that.addFormB.imgObj, form.imgObj);
						uni.showModal({
							title: '提示',
							content: '是否确认添加商户',
							success: function(res) {
								if (res.confirm) {
									that.$request.httpTokenRequest({
										url: '/admin/general/setUser',
										method: 'POST',
										data: {
											// 代理商id
											agent_id: 1,
											status: 0,
											user_name: that.addFormA.user_name,
											nick_name: that.addFormA.nick_name,
											email: that.addFormA.email,
											mobile: that.addFormA.mobile,
											tel: that.addFormA.tel,
											merchant_type: that.merType,
											cert_type: that.addFormA.cert_type,
											legal_person: that.addFormA.legal_person,
											legal_person_id: that.addFormA.legal_person_id,
											id_card_start_date: that.addFormA.id_card_start_date,
											id_card_end_date: that.addFormA.id_card_end_date,
											org_num: that.addFormA.org_num,
											address: that.addFormA.address,
											linkman: that.addFormA.linkman,
											link_phone: that.addFormA.link_phone,
											bank_name: that.addFormB.bank_name,
											bank_code: that.addFormB.bank_code,
											account_name: that.addFormB.account_name,
											account_no: that.addFormB.account_no,
											settle_bank_type: that.addFormB.settle_bank_type,
											settle_type: that.addFormB.settle_type,
											merchant_category: that.addFormB.merchant_category,
											industry_type_code: that.addFormB.industry_type_code,
											service_rate: that.addFormB.service_rate,
											settlementPeriod: that.addFormB.settlementPeriod,
											note:form.note?form.note:'',
											userFile: JSON.stringify(imgObj)
										},
										// needToken: false,
									}).then(res => {
										if (res.data.code === 200) {
											uni.showToast({
												title:'添加成功',
												icon:'success',
												duration:2000
											});
											uni.removeStorageSync('addFormA');
											uni.removeStorageSync('addFormB');
											uni.navigateBack({
												delta:4
											});
										}else{
											uni.showToast({
												title:res.data.msg,
												icon:'none',
												duration:2000
											})
										}
									}, error => {
									});
								} else if (res.cancel) {

								}
							}
						});
					}
				});
			},


		}
	}
</script>

<style lang="scss" scoped>
	.container {
		width: 96%;
		margin: 0 auto;
		padding-bottom: 2rem;

		.btns-area {
			margin-top: 2rem;
		}
	}
</style>
